package com.dy.lock.screen.component.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dy.lock.screen.component.MaterialComponent;
import com.dy.lock.screen.constants.TransmissionCode;
import com.dy.lock.screen.exception.ServerDisponseException;
import com.dy.lock.screen.pojo.MaterialInfo;
import com.dy.lock.screen.pojo.ShopTagInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.MaterialService;
import com.dy.lock.screen.service.ShopTagService;
import com.dy.lock.screen.utils.CommonUtil;
import com.dy.lock.screen.utils.LocationUtil;
import com.dy.lock.screen.vo.MaterialVo;
import com.google.gson.Gson;

@Component("materialComponent")
public class MaterialComponentImpl implements MaterialComponent {
	private static final Logger log = Logger
			.getLogger(MaterialComponentImpl.class);
	private Gson gson;

	@Resource(name = "materialService")
	private MaterialService materialService;
	@Resource(name = "shopTagService")
	private ShopTagService shopTagService;

	public MaterialComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<MaterialInfo> list(String body, Head head) {
		MaterialVo vo = gson.fromJson(body, MaterialVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Float lng = vo.getLng();
			Float lat = vo.getLat();
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("pageStart", pageStart);
			paraMap.put("pageEnd", pageEnd);
			paraMap.put("materialTag", vo.getAdTagType());
			paraMap.put("provinceName", vo.getProvinceName());
			paraMap.put("cityName", vo.getCityName());
			paraMap.put("status", "1");
			paraMap.put("materialTitle", vo.getMaterialTitle());
			paraMap.put("isHome", vo.getIsHome());
			paraMap.put("tagId", vo.getTagId());
			if (StringUtils.isNoneBlank(vo.getTagId())) {
				calTagSearchTimes(vo.getTagId());
			}
			List<MaterialInfo> materialList = this.materialService.queryPage(
					paraMap, head);
			if (materialList != null && !materialList.isEmpty()) {
				for (MaterialInfo info : materialList) {
					if (StringUtils.isNotBlank(info.getMaterialPic())) {
						info.setMaterialPic(CommonUtil.getUrlPrefix()
								+ info.getMaterialPic());
					}
					if (StringUtils.isNotBlank(info.getVideoPath())) {
						info.setVideoPath(CommonUtil.getUrlPrefix()
								+ info.getVideoPath());
					}
					if (StringUtils.isNotBlank(info.getShopHeader())) {
						info.setShopHeader(CommonUtil.getUrlPrefix()
								+ info.getShopHeader());
					}
					if (StringUtils.isNotBlank(info.getHeaderPath())) {
						info.setHeaderPath(CommonUtil
								.getUrlPrefixApi("photo.url.prefix.path.api")
								+ info.getHeaderPath());
					}
					if (lng != null && lat != null && info.getLng() != null
							&& info.getLat() != null) {
						double distance = LocationUtil.calDistance(lat, lng,
								info.getLat(), info.getLng());
						info.setDistance(distance);
					}
				}
				return materialList;
			} else {
				return new ArrayList<MaterialInfo>();
			}

		} else {
			log.info("requestId:" + head.getRequestId() + ",获取素材列表时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	/**
	 * 计算标签搜索次数
	 * 
	 * @param tagId
	 */
	private synchronized void calTagSearchTimes(String tagId) {
		ShopTagInfo tag = this.shopTagService.queryByPrimartKey(tagId);
		if (tag != null) {
			Integer searchTimes = tag.getSearchTimes();
			int st = CommonUtil.add(searchTimes, 1);
			ShopTagInfo sti = new ShopTagInfo();
			sti.setTagId(tagId);
			sti.setSearchTimes(st);
			sti.setUpdateTime(new Date());
			this.shopTagService.update(sti);
		}

	}
}
